﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.DBUtility;

namespace ZG_member.DAL
{
    public class goodsInfo
    {
        //添加数据
        public bool add(ZG_member.Model.goodsInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into goodsInfo(");
            strSql.Append("gid,goodscount,goodsprice,selltime,zhekou,wasteBookId)");
            strSql.Append("values (");
            strSql.Append("@gid,@goodscount,@goodsprice,@selltime,@zhekou,@wasteBookId)");
            SqlParameter[] parameters = {
					 
					new SqlParameter("@gid", SqlDbType.VarChar,50),
					new SqlParameter("@goodscount", SqlDbType.Int,4),
					new SqlParameter("@goodsprice", SqlDbType.Money,200),
					new SqlParameter("@selltime", SqlDbType.DateTime,200),
                    new SqlParameter("@zhekou", SqlDbType.NVarChar,200),
                    new SqlParameter("@wasteBookId", SqlDbType.Int,10)};

            parameters[0].Value = model.Gid;
            parameters[1].Value = model.Goodscount;
            parameters[2].Value = model.Goodsprice;
            parameters[3].Value = model.Selltime;
            parameters[4].Value = model.Zhekou;
            parameters[5].Value = model.wasteBookId;


            object obj = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

            if (obj == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public DataSet GetListWhere(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select *");
            strSql.Append(" FROM goodsInfo");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ZG_member.Model.goodsInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update goodsInfo set ");
            strSql.Append("gid=@gid,");
            strSql.Append("goodscount=@goodscount,");
            strSql.Append("goodsprice=@goodsprice,");
            strSql.Append("selltime=@selltime,");
            strSql.Append("zhekou=@zhekou,");
            strSql.Append("wasteBookId=@wasteBookId ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@gid", SqlDbType.VarChar,50),
					new SqlParameter("@goodscount", SqlDbType.VarChar,200),
					new SqlParameter("@goodsprice", SqlDbType.VarChar,200),
					new SqlParameter("@selltime", SqlDbType.DateTime),
                    new SqlParameter("@zhekou", SqlDbType.NVarChar,200),
                    new SqlParameter("@wasteBookId", SqlDbType.Int,10),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.Gid;
            parameters[1].Value = model.Goodscount;
            parameters[2].Value = model.Goodsprice;
            parameters[3].Value = model.Selltime;
            parameters[4].Value = model.Zhekou;
            parameters[5].Value = model.wasteBookId;
            parameters[6].Value = model.Id;
            
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return DbHelperSQL.Query(strWhere.ToString());
        }

        /// <summary>
        /// 获取表中全部数据
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetAll(string strWhere)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select * from goodsInfo");
            if(strWhere.Trim()!="")
            {
                sb.Append(" where "+strWhere);
            }
            return DbHelperSQL.Query(sb.ToString());

        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id, string goodsid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from goodsInfo");
            strSql.Append(" where id=@id and gid = @goodsid");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
                    new SqlParameter("@goodsid", SqlDbType.VarChar,50)
};
            parameters[0].Value = id;
            parameters[1].Value = goodsid;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 获取wasteBook的最大Id（在会员消费时用到）
        /// </summary>
        /// <returns></returns>
        public int GetwasteBookMaxId()
        {
            string sql = "select MAX(id)as Maxid from wasteBook";
            DataSet ds = DbHelperSQL.Query(sql.ToString());
            return Convert.ToInt32(ds.Tables[0].Rows[0]["Maxid"]);
        }
        public bool Deletewhere(string sql)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from goodsInfo ");
            if (sql.Trim()!="")
            {
                strSql.Append(" where "+sql);
            }
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
